package com.test.daily.leetcode.y2022.m05.day0515.v04;

/**
 * @author Tom on 2022/5/15
 */
public class Solution {
    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(1);
        head.next.next = new Node(1);
        head.next.next.next = new Node(3);
        head.next.next.next.next = new Node(2);
        head.next.next.next.next.next = new Node(1);
        head.next.next.next.next.next.next = new Node(4);
        head.next.next.next.next.next.next.next = new Node(1);
        Node tmp = deleteGivenValue(head,1);
        while(tmp != null){
            System.out.print(" " + tmp.val);
            tmp = tmp.next;
        }
    }
    public static class Node {
        int val;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public static Node deleteGivenValue(Node head, int givenValue) {
        if (head == null) {
            return head;
        }
        while (head != null) {
            if (head.val == givenValue) {
                head = head.next;
            }else {
                break;
            }
        }
        Node cur = head.next;
        Node pre = head;
        while (cur != null) {
            if (cur.val == givenValue) {
                pre.next = cur.next;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return head;
    }
}
